﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class SendToBackOperation : OperationBase
    {
        private DesignView _designView;
        private Part _selectedPart;
        private int _oldIndex;
        private int _newIndex;
        public SendToBackOperation(DesignView designView, Part part)
        {
            _designView = designView;
            _selectedPart = part;
            IPartContainer partContainer = part.ParentPart as IPartContainer;
            _oldIndex = partContainer.GetPartIndex(part);
            _newIndex = partContainer.GetSendToBackIndex(part);
        }
        protected override void OnDo()
        {
            int index = _newIndex;
            IPartContainer partContainer = _selectedPart.ParentPart as IPartContainer;
            partContainer.BringPartTo(_selectedPart, index);
            Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, UIContext.Current.CurrentEditedNode.ID);
            if (_selectedPart.ParentPart is ScenePart)
            {
                Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                scene.BringPartTo(area, index);
            }
            else if (_selectedPart.ParentPart is AreaPart)
            {
                Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.ParentPart.XFullName);
                if (_selectedPart is AreaPart)
                {
                    Designtime.Metadata.Area areaChild = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                    area.BringPartTo(areaChild, index);
                }
                else
                {
                    Designtime.Metadata.Part part = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                    area.BringPartTo(part, index);
                }
            }
            else if (_selectedPart.ParentPart is Part)
            {
                Designtime.Metadata.Part partParent = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.ParentPart.XFullName);
                Designtime.Metadata.Part partChild = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                partParent.BringPartTo(partChild, index);
            }
            Event.UI.UIEventBus.Publish(new Event.UI.UIChangePartZIndex(_selectedPart.XFullName, index));
        }

        protected override void OnUndo()
        {
            IPartContainer partContainer = _selectedPart.ParentPart as IPartContainer;
            partContainer.BringPartTo(_selectedPart, _oldIndex);
            Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, UIContext.Current.CurrentEditedNode.ID);
            if (_selectedPart.ParentPart is ScenePart)
            {
                Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                scene.BringPartTo(area, _oldIndex);
            }
            else if (_selectedPart.ParentPart is AreaPart)
            {
                Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.ParentPart.XFullName);
                if (_selectedPart is AreaPart)
                {
                    Designtime.Metadata.Area areaChild = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                    area.BringPartTo(areaChild, _oldIndex);
                }
                else
                {
                    Designtime.Metadata.Part part = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                    area.BringPartTo(part, _oldIndex);
                }
            }
            else if (_selectedPart.ParentPart is Part)
            {
                Designtime.Metadata.Part partParent = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.ParentPart.XFullName);
                Designtime.Metadata.Part partChild = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _selectedPart.XFullName);
                partParent.BringPartTo(partChild, _oldIndex);
            }
            Event.UI.UIEventBus.Publish(new Event.UI.UIChangePartZIndex(_selectedPart.XFullName, _oldIndex));
        }
    }
}
